package com.reggie.controller.admin;

import com.reggie.dto.DishDTO;
import com.reggie.dto.DishPageQueryDTO;
import com.reggie.entity.Dish;
import com.reggie.result.PageResult;
import com.reggie.result.R;
import com.reggie.service.DishService;
import com.reggie.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关类接口")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    //缓存处理注入Redis
    @Autowired
    private RedisTemplate redisTemplate;


    //新增菜品
    @PostMapping
    @ApiOperation("新增菜品")
    public R<String> addDish(@RequestBody DishDTO dishDTO){
        log.info("新增菜品....");
        dishService.addDish(dishDTO);

        //菜品新增 删除缓存中的旧数据
        Long categoryId = dishDTO.getCategoryId();
        String key = "dish_"+ categoryId;
        cleanCache(key);


        return R.success();
    }

    //菜品的分页查询
    @GetMapping("/page")
    @ApiOperation("分页查询")
    //前端传来的请求数据如果是query位置的就是地址栏类型的 不需要用@RequestBody
    public R<PageResult> page( DishPageQueryDTO dishPageQueryDTO){
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);

        return R.success(pageResult);
    }

    //菜品的批量删除
    @DeleteMapping
    @ApiOperation("菜品批量删除")
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("菜品批量删除：{}", ids);
        dishService.deleteBatch(ids);

        //这是批量删除菜品  所以删除所有的缓存
        cleanCache("dish_*");

        return R.success();
    }

    /**
     * 根据id查询菜品和关联的口味数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品和关联的口味数据")
    public R<DishVO> getById(@PathVariable Long id){
        return R.success(dishService.getByIdWithFlavor(id));
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public R<String> update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品：{}", dishDTO);
        dishService.updateWithFlavor(dishDTO);

        //修改菜品 也删除所有的缓存
        cleanCache("dish_*");

        return R.success();

    }

    /**
     * 菜品起售、停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售、停售")
    public R<String> startOrStop(@PathVariable Integer status, Long id){
        dishService.startOrStop(status,id);

        //起售停售 也删除所有的菜品 缓存
        cleanCache("dish_*");

        return R.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public R<List<Dish>> list(Long categoryId){
        List<Dish> list = dishService.list(categoryId);
        return R.success(list);
    }



    //封装清理Redis缓冲的方法
    public void cleanCache(String patten){
        Set keys = redisTemplate.keys(patten);
        redisTemplate.delete(keys);
    }
}
